package cn.dengta.webapp.user.model;

import java.util.*;

import cn.dengta.common.context.I18N;
import cn.dengta.common.model.*;
import cn.dengta.context.model.UserEnumI18nKey;
import lombok.Getter;
import me.codeplayer.util.EasyDate;

@Getter
public enum TaskType implements ValueProxyImpl<TaskType, Integer> {

	/** 限定 */
	FOREVER(0, "限定", UserEnumI18nKey.TASK_TYPE_FOREVER, null, true),
	/** 每日 */
	DAILY(1, "每日", UserEnumI18nKey.TASK_TYPE_DAILY, Calendar.DAY_OF_MONTH, true),
	/** 每周 */
	WEEKLY(2, "每周", UserEnumI18nKey.TASK_TYPE_WEEKLY, Calendar.DAY_OF_WEEK, false),
	/** 每月 */
	MONTHLY(3, "每月", UserEnumI18nKey.TASK_TYPE_MONTHLY, Calendar.MONTH, false),
	//
	;

	// 定义私有变量
	public final Integer value;
	public final String desc;
	public final boolean show;
	public final Integer calendar;
	final ValueProxy<TaskType, Integer> proxy;

	// 构造函数，枚举类型只能为私有
	TaskType(Integer value, String label, String desc, Integer calendar, boolean show) {
		this.value = value;
		this.desc = desc;
		this.show = show;
		this.calendar = calendar;
		this.proxy = new ValueProxy<>(this, value, label);
	}

	public String getDesc() {
		return I18N.msg(desc);
	}

	public TimeInterval getTimeInterval(Date baseTime) {
		switch (this) {
			case DAILY:
			case MONTHLY:
				return TimeInterval.of(baseTime, this.getCalendar());
			case WEEKLY:
				final EasyDate d = new EasyDate(baseTime);
				d.addDay(1 - d.getWeekDay()); // 周一
				return new TimeInterval(d.beginOf(Calendar.DAY_OF_MONTH).toDate(), d.addDay(6).endOf(Calendar.DAY_OF_MONTH).toDate(), -1, -1);
			default:
		}
		return null;
	}

	public static TaskType[] CACHE = Arrays.stream(getValues()).filter(TaskType::isShow).toArray(TaskType[]::new);

	public static TaskType[] getValues() {
		return ValueProxy.getCachedArray(TaskType.class, TaskType::values);
	}

	public static TaskType of(Integer value) {
		return FOREVER.getValueOf(value);
	}

}
